package cn.net.greenet.scheduler;

import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Dpiendpointbean;
import cn.net.greenet.db.bean.Messageincominglog;
import cn.net.greenet.db.bean.Versionreported;
import cn.net.greenet.device.send.policy.AddSendPolicyUntil;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * 自动响应DPI策略同步请求 请求与响应式响应 每分钟自动监听messageincominglog 表 判断messageType为0xC2
 * DPI策略同步请求 然后响应
 *
 * @author liangdelin
 */
public class AutoAckPolicySynScheduler {

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static AutoAckPolicySynScheduler instance = null;

    private Timer timer = new Timer(AutoAckPolicySynScheduler.class.getName());

    private AutoAckPolicySynScheduler() {

    }

    public static AutoAckPolicySynScheduler getInstance() {
        if (instance == null) {
            instance = new AutoAckPolicySynScheduler();
        }
        return instance;
    }

    public void start() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                Session session = null;
                try {
                    SessionFactory sf = HibernateUtil.getSessionFactory();
                    sf.getCache().evictEntityRegion(Messageincominglog.class);
                    sf.getCache().evictEntityRegion(Versionreported.class);
                    sf.getCache().evictEntityRegion(Dpiendpointbean.class);
                    session = sf.openSession();
                    List<Dpiendpointbean> l = session.createQuery("from Dpiendpointbean").list();
                    for (Dpiendpointbean bean : l) {
                        try {
                            System.out.println(sdf.format(new Date()) + "\t" + bean.getName() + "\t" + bean.getId());
                            String json_device_policy = GetDPIDevicePolicyVersion.getDPIVersion(String.valueOf(bean.getId())); //获取设备的上报的版本信息
                            String json = SynPolicyChangeJson.changeJson(String.valueOf(bean.getId()), json_device_policy);

                            JSONObject jo = JSONObject.fromObject(json);
                            List<Map<String, String>> data = new ArrayList<>();  //data
                            JSONArray jsonArr = jo.getJSONArray("data");
                            Map<String, String> dataMap = null;
                            for (int i = 0; i < jsonArr.size(); i++) {
                                dataMap = new HashMap<String, String>();
                                dataMap.put("message_type", jsonArr.getJSONObject(i).getString("message_type"));
                                dataMap.put("instance", jsonArr.getJSONObject(i).getString("instance"));
                                dataMap.put("dpi_edition", jsonArr.getJSONObject(i).getString("dpi_edition"));
                                data.add(dataMap);
                            }
                            boolean flag = AddSendPolicyUntil.send(bean.getId(), data);
                            System.out.println("******策略同步响应*******"+flag);
                        } catch (Exception e) {
                            e.printStackTrace();
                            continue;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (session != null) {
                        session.close();
                    }
                }
            }
        }, 30000, 60000*3);
    }

//    public static void main(String[] args) {
//        AutoAckPolicySynScheduler.getInstance().start();
//    }
}
